<template>
  <div class="page-account">
    <img src="@/assets/images/login_icon.png" class="login_icon" alt="" />
    <div class="login_icon2">
      <img src="@/assets/images/login_icon2.png" alt="" />
    </div>
    <img src="@/assets/images/login_icon3.png" class="login_icon3" alt="" />
    <div class="container">
      <!-- <swiper :options="swiperOption" class="swiperPross" v-if="fullWidth > 768">
        <swiper-slide class="swiperPic" v-for="(item, index) in swiperList" :key="index">
          <img :src="item.slide" alt="" />
        </swiper-slide>
        <div class="swiper-pagination" slot="pagination"></div>
      </swiper> -->
      <div class="index_from page-account-container from-wh">
        <p class="page-account-top-logo">欢迎登录</p>
        <el-form ref="formInline" :model="formInline" :rules="ruleInline" @keyup.enter="handleSubmit('formInline')">
          <el-form-item prop="username">
            <div class="input">
              <!-- <p v-if="formInline.username">请输入您的账号</p> -->
              <el-input
                type="text"
                v-model="formInline.username"
                prefix="ios-contact-outline"
                placeholder="请输入用户名"
                size="large"
              />
            </div>
          </el-form-item>
          <el-form-item prop="password">
            <!-- <p v-if="formInline.password">请输入密码</p> -->
            <div class="input">
              <el-input
                type="password"
                v-model="formInline.password"
                placeholder="请输入密码"
                size="large"
                show-password
              />
            </div>
          </el-form-item>
          <!-- <p v-if="formInline.code">请输入验证码</p> -->
          <!-- <el-form-item prop="code">
            
            <div class="input">
              <div class="code">
                <el-input
                  type="text"
                  v-model="formInline.code"
                  prefix="ios-keypad-outline"
                  placeholder="请输入验证码"
                  size="large"
                />
                <img src="@/assets/images/login_icon4.png" class="pictrue" />
              </div>
            </div>
          </el-form-item> -->
          <el-form-item class="pt10">
            <el-button type="primary" :loading="loading" size="large" @click="handleSubmit('formInline')" :class="['btn',{btn2 : formInline.username && formInline.password}]"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <Verify
      @success="success"
      captchaType="blockPuzzle"
      :imgSize="{ width: '330px', height: '155px' }"
      ref="verify"
    ></Verify>
  </div>
</template>
<script>
import { AccountLogin, loginInfoApi } from '@/api/account';
import { getWorkermanUrl } from '@/api/kefu';
import { setCookies } from '@/libs/util';
import '@/assets/js/canvas-nest.min';
import Verify from '@/components/verifition/Verify';
import { PrevLoading } from '@/utils/loading.js';
import { formatFlatteningRoutes, findFirstNonNullChildren } from '@/libs/system';

export default {
  components: {
    Verify,
  },
  data() {
    return {
      fullWidth: document.documentElement.clientWidth,
      swiperOption: {
        pagination: '.swiper-pagination',
        autoplay: true,
      },
      loading: false,
      isShow: false,
      autoLogin: true,
      imgcode: '',
      formInline: {
        username: '',
        password: '',
      },
      ruleInline: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
      login_captcha: 0,
      // jigsaw: null,
      login_logo: '',
      swiperList: [],
      defaultSwiperList: require('@/assets/images/sw.jpg'),
      key: '',
      copyright: '',
      version: '',
    };
  },
  created() {
    const _this = this;
    document.onkeydown = function () {
      if (_this.$route.name === 'login') {
        let key = window.event.keyCode;
        if (key === 13) {
          _this.handleSubmit('formInline');
        }
      }
    };
    // window.addEventListener('resize', this.handleResize);
  },
  watch: {
    fullWidth(val) {
      // 为了避免频繁触发resize函数导致页面卡顿，使用定时器
      if (!this.timer) {
        // 一旦监听到的screenWidth值改变，就将其重新赋给data里的screenWidth
        this.screenWidth = val;
        this.timer = true;
        let that = this;
        setTimeout(function () {
          // 打印screenWidth变化的值
          that.timer = false;
        }, 400);
      }
    },
    $route(n) {},
  },
  mounted: function () {
    this.$nextTick(() => {
      // /* eslint-disable */
      let that = this;
      // this.jigsaw = jigsaw.init({
      //   el: this.$refs.captcha,
      //   onSuccess() {
      //     that.modals = false;
      //     that.closeModel();
      //   },
      //   onFail: this.closefail,
      //   onRefresh() {},
      // });
      // if (this.screenWidth < 768) {
      //   document.getElementsByTagName('canvas')[0].removeAttribute('class', 'index_bg');
      // } else {
      //   document.getElementsByTagName('canvas')[0].className = 'index_bg';
      // }
      // this.swiperData();
    });
  },
  methods: {
    swiperData() {
      loginInfoApi()
        .then((res) => {
          window.document.title = `${res.data.site_name} - 登录`;
          localStorage.setItem('ADMIN_TITLE', res.data.site_name || '');
          this.$store.commit('setAdminTitle', res.data.site_name);
          let data = res.data || {};
          this.login_logo = data.login_logo ? data.login_logo : require('@/assets/images/logo.png');
          this.swiperList = data.slide.length ? data.slide : [{ slide: this.defaultSwiperList }];
          this.key = data.key;
          this.copyright = data.copyright;
          this.version = data.version;
          this.login_captcha = data.login_captcha;
        })
        .catch((err) => {
          this.$message.error(err);
          this.login_logo = require('@/assets/images/logo.png');
          this.swiperList = [{ slide: this.defaultSwiperList }];
        });
    },
    success(params) {
      this.closeModel(params);
    },
    // 关闭模态框
    closeModel(params) {
      this.isShow = false;
      // noinspection JSVoidFunctionReturnValueUsed

      this.loading = true;
      AccountLogin({
        account: this.formInline.username,
        pwd: this.formInline.password,
        key: this.key,
        captchaType: 'blockPuzzle',
        captchaVerification: params ? params.captchaVerification : '',
      })
        .then(async (res) => {
          // res.data.menus = [
          //   {
          //     id: 7,
          //     pid: 0,
          //     path: '/company/index',
          //     title: '主页',
          //     icon: 's-home',
          //     header: 'home',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          //   {
          //     id: 9,
          //     pid: 0,
          //     path: '/company/cultivate/index',
          //     title: '培训管理',
          //     icon: 'user-solid',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          //   {
          //     id: 1,
          //     pid: 0,
          //     path: '/company/resource/index',
          //     title: '平台资源',
          //     icon: 's-shop',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //     children: [
          //       {
          //         id: 10001,
          //         pid: 1,
          //         path: '/company/course/index',
          //         title: '课程管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 10002,
          //         pid: 1,
          //         path: '/company/courseware/index',
          //         title: '课件管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 10003,
          //         pid: 1,
          //         path: '/company/testPaper/index',
          //         title: '试卷管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 10004,
          //         pid: 1,
          //         path: '/company/question/index',
          //         title: '题库管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //     ],
          //   },
          //   {
          //     id: 42,
          //     pid: 0,
          //     path: '/company/publicResource/index',
          //     title: '公共资源',
          //     icon: 's-management',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //     children: [
          //       {
          //         id: 420001,
          //         pid: 42,
          //         path: '/company/publicCourse/index',
          //         title: '课程管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 420002,
          //         pid: 42,
          //         path: '/company/publicCourseware/index',
          //         title: '课件管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 420003,
          //         pid: 42,
          //         path: '/company/publicTestPaper/index',
          //         title: '试卷管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 420004,
          //         pid: 42,
          //         path: '/company/publicQuestion/index',
          //         title: '题库管理',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //     ],
          //   },

          //   {
          //     id: 43,
          //     pid: 0,
          //     path: '/company/myResource/index',
          //     title: '我的资源',
          //     icon: 's-management',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //     children: [
          //       {
          //         id: 430001,
          //         pid: 43,
          //         path: '/company/myCourse/index',
          //         title: '我的课程',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 430002,
          //         pid: 43,
          //         path: '/company/myCourseware/index',
          //         title: '我的课件',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 430003,
          //         pid: 43,
          //         path: '/company/myTestPaper/index',
          //         title: '我的试卷',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 430004,
          //         pid: 43,
          //         path: '/company/myQuestion/index',
          //         title: '我的题库',
          //         icon: 's-shop',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //     ],
          //   },
          //   {
          //     id: 44,
          //     pid: 0,
          //     path: '/company/disclose/index',
          //     title: '交底管理',
          //     icon: 's-management',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //     children: [
          //       {
          //         id: 1695,
          //         pid: 0,
          //         path: '/company/disclose/index',
          //         title: '交底列表',
          //         icon: 'mouse',
          //         is_header: 0,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 1697,
          //         pid: 0,
          //         path: '/company/discloseType/index',
          //         title: '交底分类',
          //         icon: 'mouse',
          //         is_header: 0,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //     ],
          //   },
          //   {
          //     id: 1696,
          //     pid: 0,
          //     path: '/company/offlinetraining/index',
          //     title: '线下培训',
          //     icon: 'mouse',
          //     is_header: 0,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          //   {
          //     id: 9999,
          //     pid: 0,
          //     path: '/company/position/index',
          //     title: '岗位工种管理',
          //     icon: 'document',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          //   {
          //     id: 566,
          //     pid: 0,
          //     path: '/company/staff/index',
          //     title: '人员管理',
          //     icon: 'picture',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          //   {
          //     id: 567,
          //     pid: 0,
          //     path: '/company/teacher/index',
          //     title: '教师管理',
          //     icon: 'picture',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          //   {
          //     id: 25,
          //     pid: 0,
          //     path: '/company/project/index',
          //     title: '项目管理',
          //     icon: 'cpu',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },

          //   {
          //     id: 207,
          //     pid: 0,
          //     path: '/company/setting',
          //     title: '基础设置',
          //     icon: 'document',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //     children: [
          //       {
          //         id: 1300,
          //         pid: 207,
          //         path: '/company/user/index',
          //         title: '账号管理',
          //         icon: 's-tools',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //       {
          //         id: 12,
          //         pid: 207,
          //         path: '/company/enterprise/index',
          //         title: '组织架构管理',
          //         icon: 's-tools',
          //         is_header: 1,
          //         is_show: 1,
          //         auth: ['hidden'],
          //       },
          //     ]
          //   },
          //   {
          //     id: 9999,
          //     pid: 0,
          //     path: '/company/article/index',
          //     title: '资讯列表',
          //     icon: 'document',
          //     is_header: 1,
          //     is_show: 1,
          //     auth: ['hidden'],
          //   },
          // ];
          let data = res.data;
          let expires = this.getExpiresTime(data.expires_time);
          // 记录用户登陆信息
          setCookies('uuid', data.user_info.id, expires);
          setCookies('token', data.token, expires);
          setCookies('expires_time', data.expires_time, expires);

          this.$store.commit('userInfo/uniqueAuth', data.unique_auth);
          this.$store.commit('userInfo/userInfo', data.user_info);
          // 保存菜单信息
          this.$store.commit('menus/setopenMenus', []);
          this.$store.commit('menus/getmenusNav', data.menus);

          this.$store.dispatch('routesList/setRoutesList', data.menus);
          let arr = formatFlatteningRoutes(this.$router.options.routes);
          this.formatTwoStageRoutes(arr);
          this.$store.commit('menus/setOneLvMenus', arr);
          let routes = formatFlatteningRoutes(data.menus);
          this.$store.commit('menus/setOneLvRoute', routes);
          // 记录用户信息
          this.$store.commit('userInfo/name', data.user_info.account);
          this.$store.commit('userInfo/avatar', data.user_info.head_pic);
          this.$store.commit('userInfo/access', data.unique_auth);
          this.$store.commit('userInfo/logo', data.logo);
          this.$store.commit('userInfo/logoSmall', data.logo_square);
          this.$store.commit('userInfo/version', data.version);
          this.$store.commit('userInfo/newOrderAudioLink', data.newOrderAudioLink);
          this.login_captcha = 0;
          PrevLoading.start();
          return this.$router.push({
            path: res.data.menus.length ? findFirstNonNullChildren(res.data.menus).path : this.$routeProStr + '/',
          });
        })
        .catch((res) => {
          let data = res === undefined ? {} : res;
          this.$message.error(data.msg || '登录失败');
          this.login_captcha = res.data.login_captcha;
        });
      setTimeout((e) => {
        this.loading = false;
      }, 1000);
    },
    formatTwoStageRoutes(arr) {
      if (arr.length <= 0) return false;
      const newArr = [];
      const cacheList = [];
      arr.forEach((v) => {
        if (v && v.meta && v.meta.keepAlive) {
          newArr.push({ ...v });
          cacheList.push(v.name);
          this.$store.dispatch('keepAliveNames/setCacheKeepAlive', cacheList);
        }
      });
      return newArr;
    },
    
    getExpiresTime(expiresTime) {
      let nowTimeNum = Math.round(new Date() / 1000);
      let expiresTimeNum = expiresTime - nowTimeNum;
      return parseFloat(parseFloat(parseFloat(expiresTimeNum / 60) / 60) / 24);
    },

    closefail() {
      // if (this.jigsaw) this.jigsaw.reset();
      this.$message.error('校验错误');
    },
    handleResize(event) {
      this.fullWidth = document.documentElement.clientWidth;
      if (this.fullWidth < 768) {
        document.getElementsByTagName('canvas')[0].removeAttribute('class', 'index_bg');
      } else {
        document.getElementsByTagName('canvas')[0].className = 'index_bg';
      }
    },
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.login_captcha == 1) {
            this.$refs.verify.show();
          } else {
            this.closeModel();
          }
        }
      });
    },
  },
  beforeCreate() {
    // if (this.fullWidth < 768) {
    //   document.getElementsByTagName('canvas')[0].removeAttribute('class', 'index_bg');
    // } else {
    //   document.getElementsByTagName('canvas')[0].className = 'index_bg';
    // }
  },
  beforeDestroy: function () {
    // window.removeEventListener('resize', this.handleResize);
    // document.getElementsByTagName('canvas')[0].removeAttribute('class', 'index_bg');
  },
};
</script>
<style scoped lang="stylus">
.page-account {
  display: flex;
  width: 100%;
  background: #ffffff;
  flex-direction: column;
  height: 100vh;
  overflow: auto;
  position: relative;
}

.login_icon{
  display: block;
  width 38%;
  margin-left: 10%;
  margin-top: 9vh;
  margin-bottom: 7.8vh;
}

.login_icon2{
  width: 100%;
  background: #2C5BA9;
  height: 38vh;
  display: flex;
  align-items: center;
}

.login_icon2 img{
  width 28%;
  margin-left: 11%;
}

.login_icon3{
  width 100%;
  margin-top: auto;
}

.page-account .code {
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-account .code .pictrue {
  height: 68px;
  position: absolute;
  top: 1px;
  right: 1px;
}

.swiperPross {
  border-radius: 12px 0px 0px 12px;
}

.swiperPross, .swiperPic, .swiperPic img {
  width: 510px;
  height: 100%;
}

.swiperPic img {
  width: 100%;
  height: 100%;
}

.container {
  width 31%;
  background: #FFFFFF;
border-radius: 8px;
position: absolute;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
top: 50%;
transform: translateY(-60%);
right: 12%;
padding: 0 50px;
}

.containerSamll {
  /* width: 56% !important; */
  background: #fff !important;
}

.containerBig {
  width: auto !important;
  background: #f7f7f7 !important;
}

.index_from {
  width: 100%;
}

.page-account-top-logo{
  width: 100%;
  line-height: 29px;
font-weight: 400;
font-size: 30px;
color: #000000;
margin-top: 9vh;
margin-bottom: 3vh;
}

.page-account-container {
  border-radius: 0px 6px 6px 0px;
}

.btn {
  width: 100%;
  height: 70px;
  font-weight: 400;
font-size: 22px;
background: #C2C9C9 !important;
margin-bottom: 7vh;
margin-top: 20px;
}

.btn2{
  background: #2C5BA9 !important;
}

.captchaBox {
  width: 310px;
}

input {
  display: block;
  width: 290px;
  line-height: 40px;
  margin: 10px 0;
  padding: 0 10px;
  outline: none;
  border: 1px solid #c8cccf;
  border-radius: 4px;
  color: #6a6f77;
}

#msg {
  width: 100%;
  line-height: 40px;
  font-size: 14px;
  text-align: center;
}

a:link, a:visited, a:hover, a:active {
  margin-left: 100px;
  color: #0366D6;
}

.index_from ::v-deep .ivu-input-large {
  font-size: 14px !important;
}

.index_from ::v-deep .el-input__inner{
  height: 70px;
  font-size: 19px !important;
}

.index_from ::v-deep .el-form-item{
  margin-bottom: 30px;
}
.index_from ::v-deep .el-form-item__error{
  font-size: 15px;
}

.from-wh {
  width: 100%;
}
.pull-right {
    float: right!important;
}
::v-deep .el-button--primary{
  border:none;
}
::v-deep .el-button{
  padding: 13px 20px !important;
}
.pull-right {
    float: right!important;
    color: #666;
}
.pull-right a {
    margin-left: 0;
    color: #666;
}
.footer{
  position: fixed;
  bottom: 0;
  width: 100%;
  left: 0;
  margin: 0;
  background: rgba(255,255,255,.8);
  border-top: 1px solid #e7eaec;
  overflow: hidden;
  padding: 10px 20px;
  height: 36px;
  line-height: 18px;
  z-index: 999;

}


.input > p{
  font-weight: 400;
font-size: 18px;
color: #999999;
}
</style>
